<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button plain @click="detail(1)">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      row-key="id"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column align="center" type="index" header-align="center" width="80px" label="序号">
      </el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="分类名"></el-table-column>
      <el-table-column prop="sort" label="排序" header-align="center" align="center"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <!--          <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
          <el-button type="text" plain @click="detail(2,scope.row.id)">修改</el-button>
          <el-button type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer title="增加目录" :visible.sync="drawer" :wrapperClosable="false">
      <el-form :model="dataForm" style="margin:  0  20px">
        <el-form-item label="所属分类" prop="cateId">
          <el-input
            placeholder="请选择分类"
            v-model="dataForm.cateName"
            type="text"
            readonly
          >
            <template slot="suffix">
              <el-button class="el-icon-edit" @click="checkSort" type="text">选择分类</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="dataForm.sort"></el-input>
        </el-form-item>
        <el-form-item>
          <template>
            <el-button @click="drawer = false">取消</el-button>
            <el-button @click="confirm">确定</el-button>
          </template>
        </el-form-item>
      </el-form>
      <el-dialog
        title="分类管理"
        :visible.sync="dialogFormVisible"
        :close-on-click-modal="false"
        :show-close="false"
        :close-on-press-escape="false"
        append-to-body
        width="40%"
      >
        <learn-cate
          ref="learnCate"
          v-if="learnCate"
          type="课程分类"
          :setCurrent="setCurrent"
          :current="current"
        />

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>
      </el-dialog>
    </el-drawer>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './learn-classification-add'
  import learnCate from './learncate'

  export default {
    data() {
      return {
        dialogFormVisible: false,
        dialogFormAddVisible: 1,
        drawer: false,
        learnCate: false,
        current: {},
        dataForm: {
          key: '',
          name: '',
          id: '',
          cateName: '',
          cateId: '',
          parentId: '',
          sort: 1
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,
      learnCate
    },
    mounted() {
      this.getDataList()
    },
    methods: {
      checkSort() {
        this.learnCate = true
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs.learnCate.init()
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done()
          })
          .catch(_ => {
          })
      },
      detail(type, id) {
        if (type === 1) {
          this.dataForm = {
            key: '',
            id: '',
            name: '',
            cateName: '',
            cateId: '',
            parentId: '',
            sort: 1
          }
        } else {
          this.$http({
            url: this.$http.adornUrl(`/train-admin/course/cate/info/${id}`),
            method: 'post'
          }).then(res => {
            console.log(res)
            if (res && res.code === 200) {
              this.dataForm = res.data
              this.dataForm.cateName = res.data.parentName
              this.dataForm.id = res.data.id
            } else {
              this.dataForm = {}
            }
            this.dataListLoading = false
          })
        }
        this.dialogFormAddVisible = type
        this.drawer = true
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/train-admin/course/cate/list'),
          method: 'post',
          data: {
            page: this.pageIndex,
            limit: this.pageSize
            // 'key': this.dataForm.key
          }
        }).then(res => {
          if (res && res.code === 200) {
            this.dataList = res.data
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      // 分类
      setCurrent(data) {
        console.log(data)
        this.dataForm.cateId = data.id
        this.dataForm.cateName = data.name
        // this.dataForm.parentId = data.parentId
        // if (data.id) this.current = data
      },
      confirm() {
        this.$http({
          url: this.$http.adornUrl(
            `/train-admin/course/cate/${
              this.dialogFormAddVisible === 1 ? 'save' : 'update'
            }`
          ),
          method: 'post',
          data: {
            id: this.dataForm.id,
            name: this.dataForm.name,
            sort: this.dataForm.sort,
            // parentId: parseInt(this.dialogFormAddVisible) === 1
            //   ? this.dataForm.cateId || 0
            //   : this.dataForm.parentId
            parentId: this.dataForm.cateId
          }
        })
          .then(res => {
            if (res && res.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.getDataList()
              this.dialogFormAddVisible = 0
              this.drawer = false
            } else {
              this.$message.warning(res.msg)
            }
            this.dataListLoading = false
          })
          .catch(_ => {
            this.dataListLoading = false
          })
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        // var ids = id ? [id] : this.dataListSelections.map(item => {
        //   return item.id
        // })
        // this.fullscreenLoading = true
        this.$confirm(`确定要进行删除操作吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/train-admin/course/cate/delete'),
            method: 'post',
            data: {id: id}
          }).then(res => {
            if (res && res.code === 200) {
              this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              })
              this.getDataList()
            } else {
              this.$notify.error({
                title: '提示',
                message: res.msg
              })
            }
          })
        })
      }
    }
  }
</script>

<style scoped>
</style>
